#include <stdio.h>
#include <arpa/inet.h>


/**
 * 字节序转换函数
 * @return
 */
//int main() {
//    char buf[4] = {192,168,1,2};
//    int num = *(int *)buf;
//    // 将本地数据转化为大端数据
//    int ss = htonl(num);
//    unsigned char *p = &ss;
//    printf("%d,%d,%d,%d\n",*p,*(p+1),*(p+2),*(p+3));
//
//    unsigned short a = 0x0102;
//    unsigned short b = htons(a);
//    printf("%x\n",b);
//
//    //大端转 本地
//    int sss = ntohl(num);
//    unsigned char *pp = &sss;
//    printf("%d,%d,%d,%d\n",*pp,*(pp+1),*(pp+2),*(pp+3));
//    unsigned bb = htons(a);
//    printf("%x\n",bb);
//    return 0;
//}

/**
 * 点分十进制串转化
 * @return
 */
int main() {
    char buf[] = "192.168.1.2";
    unsigned int num = 0;
    inet_pton(AF_INET,buf,&num);
    unsigned char *p = &num;
    printf("%d,%d,%d,%d\n",*p,*(p+1),*(p+2),*(p+3));
    char ip[16] = "";
    inet_ntop(AF_INET,&num,ip,16);
    printf("%s\n",ip);
    return 0;
}

